import { reactive } from 'vue'
//校验函数
export const validateusername = (rule, value, callback) => {
    let reg = /^[a-zA-Z0-9_-]{3,12}$/
    if(reg.test(value)){
      callback()
    }else{
      callback(new Error('必须是3~12位的英文字母、数字、下划线或短横线'))
    }
  }
export  const validatepassword = (rule, value, callback) => {
    if(value){
      callback()
    }else{
      callback(new Error('密码不能为空'))
    }
  }
export  const validatecapture = (rule, value, callback) => {
    if(value.length>=5){
    // callback(new Error('验证码必须大于五位数'))
    callback()
    }else{
    //   callback()
      callback(new Error('验证码错误'))
    }
    console.log('内容',value)
  }
export  const validatenumber = (rule, value, callback) => {
    let reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
    if(reg.test(value)){
        callback()
    }else{
        callback(new Error('请输入正确的手机号'))
    }
}
export  const validateidentify = (rule, value, callback) => {
    if(value){
        callback()
    }else{
        callback(new Error('验证码不能为空'))
    }
}
  //校验规则
export const rules = reactive({
    username: [{ validator: validateusername, trigger: 'blur' }],
    password: [{ validator: validatepassword, trigger: 'blur' }],
    capture: [{ validator: validatecapture, trigger: 'blur' }],
})
export const messageRules = reactive({
    number: [{ validator: validatenumber, trigger: 'blur' }],
    identify: [{ validator: validateidentify, trigger: 'blur' }],
})
/**
 * 
 * @param {Object} formEl 表单的引用对象
 * @param {Function} successCallback 表单校验通过的回调函数
 * @param {Function} faildCallback 表单校验失败的回调函数
 */
export const useSubmitForm = (successCallback,faildCallback)=>(formEl) => {
if (!formEl) return
formEl.validate((valid) => {
    //登入请求
    if (valid) {
    // console.log('submit!')
    if(successCallback){
        successCallback()
    }
    } else {
    // console.log('error submit!')
    if(faildCallback){
        faildCallback(false)
    }
    return false
    }
})
}
